package cn.itsource.controller;

import cn.itsource.bean.Shop;
import cn.itsource.query.ShopQuery;
import cn.itsource.service.IShopService;
import cn.itsource.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-13 16:23:00
 */
@Slf4j
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private IShopService shopService;



    @PostMapping("/findPage")
    public JsonResult findPage(@RequestBody ShopQuery query){
//        log.error(query.toString());
        return shopService.getPage(query);
    }


    @PostMapping("/add")
    public JsonResult addOrUpdate(@RequestBody Shop shop){
//        log.error(shop.toString());
        if (shop.getId()==null){
            shopService.save(shop);
            return JsonResult.success("新增成功");
        }else {
            shopService.updateById(shop);
            return JsonResult.success("修改成功");
        }
    }


    /**
     * 调整店铺状态
     * @param shop
     * @return
     */
    @PostMapping("/state")
    public JsonResult changeState(@RequestBody Shop shop){
//        log.info("state,{}",department.toString());
        JsonResult result = shopService.updateState(shop);
        return result;
    }


    @PostMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
//        log.error(id.toString());
        shopService.removeById(id);
        return JsonResult.success("删除成功");
    }

    @PostMapping("/delete")
    public JsonResult deleteAll(@RequestBody ShopQuery query){
        if (query.getIds()==null){
            return JsonResult.error("请选择！");
        }
        shopService.removeByIds(query.getIds());
        return JsonResult.success("删除成功");
    }

//    @PostMapping("/upload")
//    public JsonResult updateFile(MultipartFile file){
//        String uploadFile = fileService.uploadFile(file);
////        log.error(uploadFile);
//        if (uploadFile!=null){
//            return JsonResult.success(uploadFile);
//        }else {
//            return JsonResult.error("上传失败");
//        }
//    }
//
//    @DeleteMapping("/deleteFile/{fileName}")
//    public JsonResult delete(@PathVariable("fileName") String fileName){
////        log.error(fileName);
//        if (fileService.deleteFile(fileName)) {
//            return JsonResult.success("OK");
//        }
//        return JsonResult.error("删除失败");
//    }


    /**
     * 商家入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        return shopService.settlement(shop);
    }

    @PostMapping("/settlementAnge")
    public JsonResult settlementAnge(@RequestBody Shop shop){
        return shopService.settlementAnge(shop);
    }


    /**
     * 店铺过审触发的请求
     * @return
     */
    @PostMapping("/pass")
    public JsonResult pass(@RequestBody Shop shop){
        return shopService.passShop(shop);
    }


}
